﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTB_CTK34;
using BLL;
using Entities;

namespace QLTB_CTK34
{
    public partial class FormThemTaiKhoan : Form
    {
        public FormThemTaiKhoan()
        {
            InitializeComponent();
            this.MaximizeBox = false;
        }
        public TaiKhoan TaiKhoanThem;
        List<PhanQuyenSuDung> dspqsd;
        LoaiQuyen lq = LoaiQuyen.SinhVien;
        /// <summary>
        /// click vao button phan quyen sử dụng hiển thị form plsd.
        /// </summary>
        private void btnPhanQuyen_Click(object sender, EventArgs e)
        {
           FrmThemPhanQuyenSuDung frmthempqsd=new FrmThemPhanQuyenSuDung(mtxtMaTaiKhoan.Text,txtTenTK.Text,cbbChucvu.Text,lq);
            frmthempqsd.ShowDialog();
            dspqsd = frmthempqsd.dspqsd;
        }
        /// <summary>
        /// click vao button thêm hệ thống xử lý để thêm tài khoản.
        /// </summary>
        private void btnThem_Click(object sender, EventArgs e)
        {
            TaiKhoanThem = ThemTK();
            var kq = QLTaiKhoan.Insert(TaiKhoanThem, ThemTT(), txtMatKhau2.Text,dspqsd);
            kq.Show();
            if(kq.Success)
            {
				QLNhatKy.ThemNhatKy("Thêm tài khoản");
                DialogResult = DialogResult.OK;
            }
        }

        private TaiKhoan ThemTK()
        {
            TaiKhoan tk = new TaiKhoan();
            tk.MaTK = mtxtMaTaiKhoan.Text;
            tk.TenDangNhap = txtTenTK.Text;
            tk.MatKhau = txtMatKhau.Text;
            tk.TrangThai = "offline";
            tk.LanCuoiDangNhap = (DateTime?) null;
            return tk;
        }

        private ThongTinNguoiDung ThemTT()
        {
            ThongTinNguoiDung ttcn = new ThongTinNguoiDung();
            ttcn.Ho = txtHo.Text;
            ttcn.Ten = txtTen.Text;
            ttcn.NgaySinh = dtpNgaySinh.Value;//string.IsNullOrEmpty(txtNgaySinh.Text) ? (DateTime?)null : DateTime.Parse(txtNgaySinh.Text);
			ttcn.SDT = string.IsNullOrEmpty(txtDT.Text) ? (int?)null : int.Parse(txtDT.Text);
            ttcn.CMND = txtCMND.Text;
            ttcn.ChucVu = cbbChucvu.Text;
            ttcn.MaTK = mtxtMaTaiKhoan.Text;
            return ttcn;
        }
        /// <summary>
        /// click vao button mặc định trả các textbox về trạng thái ban đầu.
        /// </summary>
        private void btnMacDinh_Click(object sender, EventArgs e)
        {
            mtxtMaTaiKhoan.ResetText();
            txtHo.ResetText();
            txtTen.ResetText();
            dtpNgaySinh.ResetText();
            txtDT.ResetText();
            cbbChucvu.ResetText();
            txtCMND.ResetText();
            txtTenTK.ResetText();
            txtMatKhau.ResetText();
            txtMatKhau2.ResetText();
        }
        /// <summary>
        /// click vao button thoát form sẽ đóng lại.
        /// </summary>
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ThemTaiKhoan_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// sự liện cbb chức vụ thay đổi thông tin cho txtMaTK.
        /// </summary>
        private void cbbChucvu_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbbChucvu.Text)
            {
                case "Coi Phòng Lab":
                    mtxtMaTaiKhoan.Mask = @"GV\C.&&&&&&&";
                    lq = LoaiQuyen.CoiPhongLab;
                    break;
                case "Bảo Trì Phòng Lab":
                    mtxtMaTaiKhoan.Mask = "GVBT.&&&&&&&";
                    lq = LoaiQuyen.BaoTriPhongLab;
                    break;
                case "Sinh Vien":
                    mtxtMaTaiKhoan.Mask = "SV.&&&&&&&";
                    lq = LoaiQuyen.SinhVien;
                    break;
                case "Admin":
                    mtxtMaTaiKhoan.Mask = @"\AD.&&&&&&&";
                    lq = LoaiQuyen.Admin;
                    break;
            }
            dspqsd = QLPhanQuyenSuDung.ThemPhanQuyen(lq).Result;
        }

        private void mtxtMaTaiKhoan_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
           
        }
    }
}
